import path from "path";
import {default as axios} from "axios";
import fs from "fs";
import * as https from "https";
import {Stream} from "stream";

/**
 * 得到输出路径
 * @param target 绝对路径
 * @param from
 * @param dist
 */
export function getDistPath(target: string, from: string, dist: string) {
    return path.join(dist, target.slice(from.length));
}

/**
 * from的文件相对于to变化后的路径
 * @param from
 * @param to
 */
export function getRelatedPath(from: string, to: string) {
    if (from.endsWith('/')) {
        return path.resolve(from, to);
    }
    return path.join(path.dirname(from), to);
}

/**
 * 获取一个文件（buffer）
 * @param url
 */
export function fetchOneFile(url) {
    return axios.get(url, {
        responseType: "arraybuffer",
    }).then((res) => {
        return res.data as ArrayBuffer;
    }, () => {
        return null;
    })
}
